#include "vtor_spi.h"

#ifdef __VTOR_SPI__

// 加入用户工程宏
#ifdef __DEMO_PROJECT__

#include "main.h"

// 加入需要依赖的变量
#define	W25QXX_VTOR_SPI_CLK_Port		GPIOA
#define W25QXX_VTOR_SPI_CLK_Pin			GPIO_PIN_5
#define	W25QXX_VTOR_SPI_MOSI_Port		GPIOA
#define W25QXX_VTOR_SPI_MOSI_Pin		GPIO_PIN_7
#define	W25QXX_VTOR_SPI_MISO_Port		GPIOA
#define W25QXX_VTOR_SPI_MISO_Pin		GPIO_PIN_6
#define	W25QXX_VTOR_SPI_CS_Port			GPIOA
#define W25QXX_VTOR_SPI_CS_Pin			GPIO_PIN_2

VtorSpi vtorSpiArray[2] = {0};
VtorSpi * w25qxxVtorSpi = {0};
VtorSpi* lcdSpi = {0}; // 1.8寸 128*160 RGB TFT LCD ST7735 NOMEN_TEC

void VtorSpi_WriteClk(VtorSpi* spi, uint8_t level)
{
	if(spi == w25qxxVtorSpi)
	{
		HAL_GPIO_WritePin(W25QXX_VTOR_SPI_CLK_Port,
			W25QXX_VTOR_SPI_CLK_Pin, (GPIO_PinState)level);
	}
	else if(spi == lcdSpi)
	{
	}
	else
	{
	}
}

void VtorSpi_WriteMosi(VtorSpi* spi, uint8_t level)
{
	if(spi == w25qxxVtorSpi)
	{
		HAL_GPIO_WritePin(W25QXX_VTOR_SPI_MOSI_Port,
			W25QXX_VTOR_SPI_MOSI_Pin, (GPIO_PinState)level);
	}
	else if(spi == lcdSpi)
	{
	}
	else
	{
	}
}

uint8_t VtorSpi_ReadMiso(VtorSpi* spi)
{
	if(spi == w25qxxVtorSpi)
	{
		return HAL_GPIO_ReadPin(W25QXX_VTOR_SPI_MISO_Port,
			W25QXX_VTOR_SPI_MISO_Pin);
	}
	else if(spi == lcdSpi)
	{
	}
	else
	{
	}
	return 0;
}

void VtorSpi_WriteCs(VtorSpi* spi, uint8_t level)
{
	if(spi == w25qxxVtorSpi)
	{
		HAL_GPIO_WritePin(W25QXX_VTOR_SPI_CS_Port,
			W25QXX_VTOR_SPI_CS_Pin, (GPIO_PinState)level);
	}
	else if(spi == lcdSpi)
	{
	}
	else
	{
	}
}








void VtorSpi_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pin = W25QXX_VTOR_SPI_CLK_Pin;
	HAL_GPIO_Init(W25QXX_VTOR_SPI_CLK_Port, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = W25QXX_VTOR_SPI_MOSI_Pin;
	HAL_GPIO_Init(W25QXX_VTOR_SPI_MOSI_Port, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = W25QXX_VTOR_SPI_CS_Pin;
	HAL_GPIO_Init(W25QXX_VTOR_SPI_CS_Port, &GPIO_InitStruct);
	
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	GPIO_InitStruct.Pin = W25QXX_VTOR_SPI_MISO_Pin;
	HAL_GPIO_Init(W25QXX_VTOR_SPI_MISO_Port, &GPIO_InitStruct);
	
	// 根据不同实例进行细分配置
	w25qxxVtorSpi = &vtorSpiArray[0];
	w25qxxVtorSpi->cpol = SPI_POLARITY_HIGH; // clk空闲为高电平


	VtorSpi_WriteCs(w25qxxVtorSpi, GPIO_LEVEL_HIGH);
	VtorSpi_SetClkIdle(w25qxxVtorSpi);
	
	
	lcdSpi = &vtorSpiArray[1];
	VtorSpi_WriteCs(lcdSpi, GPIO_LEVEL_HIGH);
	VtorSpi_SetClkIdle(lcdSpi);
}


inline void VtorSpi_Delay(uint32_t tick)
{
	//HAL_Delay(tick);
	__nop();
	__nop();
	__nop();
}


#endif // __USER_PROJECT__

#endif // __VTOR_SPI__

